<?php
namespace App\Library;

class Helper {

    private static $xml;

    /**
     * 获取当前时间
     *
     * @param string $format
     *
     * @return string
     */
    public static function getNow($format = "Y-m-d H:i:s") {
        return date($format);
    }

    /**
     * 检查是不是正确的手机号码
     *
     * @param string $mobile
     *
     * @return bool
     */
    public static function isMobile($mobile) {
        if($mobile == '') {
            return false;
        }
        return (!preg_match("/^1[3,4,5,7,8][0-9]{9}$/", $mobile)) ? false : true;
    }

    /**
     * 转化ui-datepicker到标准时间
     * @param $date
     * @return bool|string
     */
    public static function getDatepickerDateToStandardDate($date) {
        if($date == '') {
            return '';
        }
        $ops = strpos($date, 'GMT');
        if($ops > 0) {
            $date = trim(substr($date, 0, $ops));
        }
        $date = date('Y-m-d H:i:s', strtotime($date));
        if(substr($date, 0, 10) != '1970-01-01') {
            return $date;
        }
        return '';
    }

    /**
     * 数组转化成xml
     * @param $array
     *
     * @return mixed
     */
    public static function array2xml($array) {
        if(!is_object(self::$xml)) {
            self::$xml = new \SimpleXMLElement('<?xml version="1.0" encoding="utf-8"?><result></result>');
        }
        foreach($array as $k => $v) {
            if(is_array($v)) {
                array2xml($v, self::$xml->addChild($k));
            } else {
                self::$xml->addChild($k, $v);
            }
        }
        return self::$xml->asXML();
    }

    public static function formatDownloadText($text) {
        return str_replace([",", "\n", "\r"], ["，", " ", " "], $text);
    }
}